<?php

namespace app\bootstrap;

use Illuminate\Database\Events\QueryExecuted;
use support\Db;
use support\Log;
use Webman\Bootstrap;

class SqlDebug implements Bootstrap
{
    public static function start($worker)
    {
        // Log::info('enter start:---'. ini('app.debug_mode'));
        //var_dump(config('app.debug_mode'));
        if (true == config('app.debug'))
        {
            Db::connection()->listen(function (QueryExecuted $queryExecuted){
                if (isset($queryExecuted->sql) and  $queryExecuted->sql !== "select 1")
                {
                   

                    $bindings = $queryExecuted->bindings;
                    $sql = array_reduce(
                        $bindings,
                        function ($sql, $binding) {
                            return preg_replace('/\?/', is_numeric($binding) ? $binding : "'" . $binding . "'", $sql, 1);
                        },
                        $queryExecuted->sql
                    );

                     dump("[{$queryExecuted->time} ms] {$sql}");
                }
            });
        }

    }

}
